package com.canbot.u05.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.canbot.u05.R;
import com.canbot.u05.adapter.DanceAdapter;
import com.canbot.u05.bean.Brow;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 舞蹈的Fragment
 */
public class DanceFragment extends Fragment implements AdapterView.OnItemClickListener {

        public List<Brow> mbrowList;

        private ViewPager mViewPager;

        private int homePage;

        private HashMap<String, String> hashMap = new HashMap<>();

        private ToastTools toastTools;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.fragment_dance, container, false);
                mViewPager = (ViewPager) view.findViewById(R.id.vp_content_dance);
                getDate();
                mViewPager.setAdapter(pagerAdapter);
                pagerAdapter.notifyDataSetChanged();
                Logger.e("test", "onCreateView");
                mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int i, float v, int i1) {

                        }

                        @Override
                        public void onPageSelected(int i) {
                                homePage = i;
                        }

                        @Override
                        public void onPageScrollStateChanged(int i) {

                        }
                });
                mViewPager.setCurrentItem(0);
                return view;
        }

        @Override
        public void onResume() {
                super.onResume();
        }

        @Override
        public void onAttach(Activity activity) {
                super.onAttach(activity);
                toastTools = ToastTools.newInstance(getActivity());
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                handOnItemClick(i);
        }

        /**
         * 获取数据
         */
        private List<Brow> getDate() {
                //TODO path 路径 和 actionID
                mbrowList = new ArrayList<Brow>();
                Brow brow1 = new Brow(1, "小苹果", "6", "xpg.mp3", "-1");
                Brow brow2 = new Brow(2, "圆舞曲", "10227", "lsdnh.mp3", "-1");
                Brow brow3 = new Brow(3, "最炫名族风", "1006", "zxmzf.wav", "-1");
                Brow brow4 = new Brow(4, "MJ之舞", "10224", "mj.wav", "-1");
                Brow brow5 = new Brow(5, "武术表演", "10225", "dzzl_u05_019_1.wav", "-1");
                Brow brow6 = new Brow(6, "三连拍", "10218", "dzzl_u05_013.wav", "18");
                Brow brow7 = new Brow(7, "展示自己", "10234", "self_introduction_my.wav", "-1");
                Brow brow8 = new Brow(8, "沧海一声笑", "6", "chisx.mp3", "-1");
                Brow brow9 = new Brow(9, "无法停止的感觉", "10235", "music10235.mp3", "-1");
                Brow brow10 = new Brow(10, "海草海草", "10236", "seaweed_dance.mp3", "-1");
                Brow brow11 = new Brow(11, "despacito", "10237", "despacito.mp3", "-1");
                Brow brow12 = new Brow(12, "trouble maker", "10238", "trouble_maker.mp3", "-1");
                Brow brow13 = new Brow(13, "红昭愿", "10239", "hzy.mp3", "-1");

                mbrowList.add(brow1);
                mbrowList.add(brow2);
                mbrowList.add(brow3);
                mbrowList.add(brow4);
                mbrowList.add(brow5);
                mbrowList.add(brow6);
                mbrowList.add(brow7);
                mbrowList.add(brow8);
                mbrowList.add(brow9);
                mbrowList.add(brow10);
                mbrowList.add(brow11);
                mbrowList.add(brow12);
                mbrowList.add(brow13);

                return mbrowList;
        }

        private PagerAdapter pagerAdapter = new PagerAdapter() {

                @Override
                public boolean isViewFromObject(View arg0, Object arg1) {

                        return arg0 == arg1;
                }

                @Override
                public int getCount() {
                        return (mbrowList.size() + 5)/6;
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                        container.removeView((View) object);
                }

                @Override
                public Object instantiateItem(ViewGroup container, final int position) {
                        LayoutInflater inflater = LayoutInflater.from(container.getContext());
                        View view = inflater.inflate(R.layout.fragment_dance_adapter, container, false);
                        GridView mGridView = (GridView) view.findViewById(R.id.gv_content_dance);
                        List<Brow> mbrowList1 = new ArrayList<>();
                        for (int i = 0; i < getCount(); i++) {
                                if (i == position) {
                                        mbrowList1 = mbrowList.subList(i * 6, i * 6 +6< mbrowList.size() ? i * 6 + 6 : mbrowList.size());
                                }
                        }
                        DanceAdapter danceAdapter = new DanceAdapter(getActivity(), mbrowList1);
                        mGridView.setOnItemClickListener(DanceFragment.this);
                        container.addView(view);
                        mGridView.setAdapter(danceAdapter);
                        danceAdapter.notifyDataSetChanged();
                        return view;
                }

                @Override
                public int getItemPosition(Object object) {
                        return POSITION_NONE;
                }
        };

        /**
         * 处理点击事件
         *
         * @param position
         */
        private void handOnItemClick(int position) {
                Brow brow = mbrowList.get(homePage * 6 + position);
                toastTools.showText(brow.getName());

                //直接发送給头
                boolean playSound = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.DANCE_DIR + brow.getPath());
                boolean eyeMotion = MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, brow.getEyesID());
                boolean action = MsgSendUtils.sendStringMsg(MsgType.ACTION, brow.getActionId());
                if (playSound && eyeMotion && action) {
                        toastTools.showText(getString(R.string.send_successfully));
                }
                else {
                        toastTools.showText(getString(R.string.network_error));
                }


//                //通过胸口发送給头，不需要三方联网
//                hashMap.clear();
//                hashMap.put("action",brow.getActionId());
//                hashMap.put("music",brow.getPath());
//                hashMap.put("eyes",brow.getEyesID());
//                //OkHttpUtils.upLoadFile(Url, hashMap,callBack);
//                MsgSendUtils.sendStringMsgToPad(MsgType.SEND_PAD_EXECUTE_ACTION, JSON.toJSONString(brow));

        }

        OkHttpUtils.OkHttpCallBack callBack = new OkHttpUtils.OkHttpCallBack() {
                @Override
                public void onSuccess(String result) {
//                        toastTools.showText(result);
                }

                @Override
                public void onFailure() {
                }
        };

        private String Url = "http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "actionmusic";

}
